package com.jz.digital.login.set

import android.content.Context
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.jz.digital.R
import com.jz.digital.adapter.LanguageSelectAdapter
import com.jz.digital.base.BaseActivity
import com.jz.digital.dialog.CommBottomConfirmDialog
import com.jz.digital.utils.SPHelper
import com.jz.digital.utils.systembar.ImmersionBar
import com.jz.digital.view.CommonTitleBarHelper
import kotlinx.android.synthetic.main.activity_login_set.et_lan_address
import kotlinx.android.synthetic.main.activity_login_set.image_menu
import kotlinx.android.synthetic.main.activity_login_set.image_save
import kotlinx.android.synthetic.main.activity_login_set.ll_language_select
import kotlinx.android.synthetic.main.activity_login_set.recycle_view
import kotlinx.android.synthetic.main.activity_login_set.tv_language
import kotlinx.android.synthetic.main.layout_common_title.layout_parent
import kotlin.system.exitProcess

/**
 * ================================================
 * Created: WangYong
 * Date: 2024/11/19
 * Description:
 * ================================================
 */
class LoginSetActivity : BaseActivity<LoginSetPresenter>(), ILoginSetView, View.OnClickListener {
    /**
     * 语言列表
     */
    private val mLanguageList = mutableListOf<String>()

    /**
     * 选中语言位置
     */
    private var mSelectPosition = 0
    private val mAdapter: LanguageSelectAdapter by lazy {
        LanguageSelectAdapter(mLanguageList, mSelectPosition)
    }

    /**
     * 菜单是否展开
     */
    private var mMenuState = false

    override fun getLayoutResId(): Int {
        return R.layout.activity_login_set
    }

    override fun initListening() {
        super.initListening()
        image_save.setOnClickListener(this)
        image_menu.setOnClickListener(this)
    }

    override fun initViews() {
        super.initViews()
        mTitleBar.setLeftImgTint(ColorStateList.valueOf(getColor(R.color.color_366AE4)))
        et_lan_address.setText(SPHelper.getLocalLanAddress())
        et_lan_address.setPhoneHint(resources.getString(R.string.please_input_lan_address))
    }

    override fun initData() {
        super.initData()
        mLanguageList.addAll(
            mutableListOf(
                getString(R.string.chinese),
                getString(R.string.english),
                getString(R.string.vietnamese_ting_vit)
            )
        )
        initRecycleView()
    }

    private fun initRecycleView() {
        recycle_view.layoutManager = LinearLayoutManager(this)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val name = adapter.data[position] as String
            mMenuState = false
            ll_language_select.visibility = View.GONE
            image_save.visibility = View.VISIBLE

            tv_language.text = name
            mSelectPosition = position
            mAdapter.selectPosition = position
            mAdapter.notifyDataSetChanged()
        }
        recycle_view.adapter = mAdapter
    }

    companion object {
        @JvmStatic
        fun startActivity(context: Context) {
            context.startActivity(Intent(context, LoginSetActivity::class.java))
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.image_save -> {
                if (et_lan_address.phone.trim().isEmpty()) {
                    showToast(R.string.lan_address_no_empty)
                    return
                }
                if (!et_lan_address.phone.trim().startsWith("http")) {
                    showToast(R.string.input_http_start_address)
                    return
                }
                if (et_lan_address.phone.trim() == SPHelper.getLocalLanAddress()) {
                    finishAfterTransition()
                    return
                }
                val dialog = CommBottomConfirmDialog.newInstance(
                    getString(R.string.switching_lan_tips),
                    getString(R.string.text_exit),
                    getString(R.string.key_507),
                    true
                )
                dialog.setOnConfirmListener {
                    showLoadingDialog()
                    dialog.dismissAllowingStateLoss()
                    SPHelper.saveLocalLanAddress(et_lan_address.phone.trim())
                    showToast(getString(R.string.set_success))
                    Handler(Looper.getMainLooper()).postDelayed({
                        dismissLoadingDialog(false)
                        finishAffinity()
                        exitProcess(0)
                    }, 1000)

                }
                dialog.show(supportFragmentManager)
            }

            R.id.image_menu -> {
                //菜单
                if (mMenuState) {
                    mMenuState = false
                    ll_language_select.visibility = View.GONE
                    image_save.visibility = View.VISIBLE
                } else {
                    mMenuState = true
                    ll_language_select.visibility = View.VISIBLE
                    image_save.visibility = View.GONE
                }
            }
        }
    }
}